
package com.fly.demo.dao;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.fly.demo.entity.User;

public interface UserDAO extends JpaRepository<User, Long>, JpaSpecificationExecutor<User>
{
    @Query(value = "select name, age from user", nativeQuery = true)
    public List<Object[]> queryAll();
    
    // @Query(value = "select * from user", nativeQuery = true)
    // @Query(value = "from User")
    @Query("from User")
    public List<User> queryUserList();
    
    /**
     * 字段要写全，否则映射到User报错
     * 
     * @return List<User>
     */
    @Query(value = "select id, name, age from user", nativeQuery = true)
    public List<User> queryUserList2();
    
    /**
     * 按name查询
     *
     * @param name
     * @return
     */
    List<User> findByNameLike(String name);
    
    /**
     * 按name查询条数
     *
     * @param name
     * @return
     */
    Long countByName(String name);
    
    /**
     * 传统参数HQL演示
     * 
     * @param age
     * @return
     */
    @Modifying
    @Query(value = "update User set age =?1")
    int updateAge(Integer age);
    
    /**
     * 命名参数HQL演示
     * 
     * @param name
     * @param age
     * @return
     */
    @Modifying
    @Query(value = "update User set age =:age where name =:name")
    int updateAge(@Param("name") String name, @Param("age") Integer age);
}